<template>
  <el-dialog
    :title="article ? '修改文章' : '新增文章'"
    :visible="showAdd"
    @close="btnCancel"
    @open="open"
  >
    <el-form ref="form" :model="formData" :rules="rules">
      <el-form-item label="文章标题" label-width="80px" prop="title">
        <el-input v-model="formData.title"></el-input>
      </el-form-item>
      <el-form-item label-width="80px" label="文章内容" prop="articleBody">
        <!-- 富文本 -->
        <Toolbar
          style="border-bottom: 1px solid #ccc"
          :editor="editor"
          mode="simple"
        />
        <Editor
          style="height: 150px; overflow-y: hidden"
          v-model="formData.articleBody"
          :defaultConfig="editorConfig"
          @onCreated="onCreated"
          @onBlur="blurEditor"
          mode="simple"
        />
      </el-form-item>
      <el-form-item label="视频地址" label-width="80px">
        <el-input v-model="formData.videoURL" placeholder="请输入视频地址"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button @click="btnCancel">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
// 富文本编辑器导入
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import '@wangeditor/editor/dist/css/style.css'

import { add, update } from '@/api/hmmm/articles'
export default {
  name: 'ArticleAdd',
  components: { Editor, Toolbar },
  props: {
    showAdd: {
      type: Boolean,
      default: false
    },
    article: {
      type: Object
    }
  },
  data () {
    return {
      editor: null,
      editorConfig: {
        placeholder: '请输入文章内容...'
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      formData: {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    }
  },
  methods: {
    open () {
      if (this.article) {
        this.formData = { ...this.article }
      }
    },
    btnCancel () {
      this.$emit('update:article', null)
      this.$emit('update:showAdd', false)
      this.formData = { title: '', articleBody: '', videoURL: '' }
      this.$refs.form.resetFields()
    },
    onCreated (editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    blurEditor (editor) {
      this.formData.articleBody = editor.getText()
      this.$refs.form.validateField('articleBody')
    },
    async submit () {
      try {
        await this.$refs.form.validate((valid) => {
          if (!valid) return this.$message.error('内容不能为空')
        })
        if (this.formData.id) {
          await update({ ...this.formData })
          this.$message.success('修改成功')
        } else {
          await add({ ...this.formData })
          this.$message.success('新增成功')
        }
        this.$parent.initList()
      } catch {
        // (e) => {
        //   console.log(e)
        // }
      } finally {
        this.btnCancel()
      }
    }
  }
}
</script>

<style>
</style>
